﻿#include "EditorDebugConsoleView.h"

#include "Widgets/TextWidget.h"
#include "Widgets/EditorScrollWidget.h"
#include "Widgets/Positioners/VerticalPositionerWidget.h"   
#include "EditorDebugConsoleViewModel.h"

#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "Widgets/EditorImageButton.h"

void REditorDebugConsoleView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    auto ScrollWidget = NewWidget<REditorScrollWidget>(InParent);
    ScrollWidget->Anchor.Fill(InParent);
    REditorScrollWidgetWeakPtr WeakScrollWidget=ScrollWidget;

    auto ContentContainer = ScrollWidget->GetContentContainer();
    ContentContainer->Anchor.HorizontalFill(ScrollWidget);
    ContentContainer->Height=[this,WeakScrollWidget]()->float
    {
        return WeakScrollWidget->PresentSize.Y;
    };
    auto Vertical= NewWidget<RVerticalPositionerWidget>(ContentContainer);
    Vertical->Anchor.HorizontalFill(ContentContainer);
    RVerticalPositionerWidgetWeakPtr WeakVertical=Vertical;

    auto CreateLine=[this,WeakVertical](int64_t Index)
    {
        auto TextWidget=NewWidget<RTextWidget>(WeakVertical);
        TextWidget->Text=
            [this,Index]()
            {
                return OutputViewModel->GetLine(Index);
            };
        TextWidget->Color=
            [this,Index]()
            {
                return OutputViewModel->GetLineColor(Index);
            };
        
        TextWidget->Wrap=ETextWrapMethod::WrapByWidthAndAtLineEnd;
        TextWidget->Anchor.HorizontalFill(WeakVertical);

        RTextWidgetWeakPtr WeakTextWidget=TextWidget;
        TextWidget->Height=[WeakTextWidget]()->float
        {
            return WeakTextWidget->GetTextSize().Y;
        };

    };

    for(int i=0;i<OutputViewModel->GetLineNum();i++)
    {
        CreateLine(i);
    }

    OutputViewModel->OnLineNumChanged.Bind([this,WeakVertical,CreateLine]()
    {
        int64_t NewLineNum=OutputViewModel->GetLineNum();
        int64_t OldLineNum=WeakVertical->GetSubWidgetNum();
        if(NewLineNum>OldLineNum)
        {
            for(int i=OldLineNum;i<NewLineNum;i++)
            {
                CreateLine(i);
            }
        }
        else
        {
            for(int i=OldLineNum-1;i>=NewLineNum;i--)
            {
                WeakVertical->RemoveSubWidgetAt(i);
            }
        }
    });

}

void REditorDebugConsoleView::BindViewModel(TSharedObjectPtr<REditorAbilityViewModel> InViewModel)
{
    Super::BindViewModel(InViewModel);
    OutputViewModel =InViewModel;
}

void REditorDebugConsoleView::FillOperations(TSharedObjectPtr<RWidget> InTitleBar)
{
    auto ButtonBar=NewWidget<RHorizontalPositionerWidget>(InTitleBar);
    ButtonBar->Anchor.VerticalCenterReference=InTitleBar;
    ButtonBar->Anchor.RightReference=InTitleBar;
    ButtonBar->Spacing=OutputViewModel->OperationInterval;
    ButtonBar->Height=OutputViewModel->OperationButtonSize;

    ButtonBar->Width= OutputViewModel->OperationButtonSize;

    auto ClearAllButton=WHE::CreateImageButton(
        OutputViewModel->ClearAllIcon
        ,CString(U"清除输出")
        ,[this](){
            //TODO implement
        }
        ,ButtonBar
        ,OutputViewModel->OperationButtonSize
        ,OutputViewModel->OperationButtonImageSize
    );
    ClearAllButton->Anchor.VerticalFill(ButtonBar);
    ClearAllButton->Width=OutputViewModel->OperationButtonSize;

}
